package com.lb.activity.sys.controller;

import com.lb.activity.po.Dept;
import com.lb.activity.po.User;
import com.lb.activity.sys.service.DeptService;
import com.lb.activity.sys.service.RoleService;
import com.lb.activity.sys.service.UserRoleService;
import com.lb.activity.sys.service.UserService;
import com.lb.activity.util.JsonUtil;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.TreeJson;
import com.lb.activity.vo.SimpleDeptVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 001030
 */
@RestController
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @Resource
    private UserRoleService userRoleService;
    @Autowired
    private DeptService deptService;

    /**
     * 跳转页面
     * @return
     */
    @GetMapping("user")
    public ModelAndView user(){
        return new ModelAndView("user/user");
    }

    /**
     * table 数据
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("users")
    public Object users(int page,int limit,Long deptId){
        Map<String,Object> map = new ConcurrentHashMap<>(2);
        map.put("page",page);
        map.put("limit",limit);
        if(deptId != null){
            map.put("groupId",deptId);
        }
        ResultUtil re  = userService.userList(map);
        return re;
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @GetMapping("toUserAdd")
    public ModelAndView toUserAdd(){
        List<Dept> list = deptService.AllDepts();
        List<SimpleDeptVO> depts = SimpleDeptVO.parse(list);
        ModelAndView mv = new ModelAndView("user/user-add");
        mv.addObject("depts", depts);
        return mv;
    }

    /**
     * 添加操作
     * @param user
     * @return
     */
    @PostMapping("userAdd")
    public Object addUser(User user){
        ResultUtil resultUtil = userService.addUser(user);
        return resultUtil;
    }

    /**
     * 改变用户状态
     * @param userId
     * @return
     */
    @PostMapping("changeUserStatus")
    public Object changeUserStatus(Long userId){
        ResultUtil resultUtil = userService.changeUserStatus(userId);
        return resultUtil;
    }

    /**
     * 跳转到角色的页面
     * @param id
     * @return
     */
    @GetMapping("assignUser")
    public ModelAndView assignUser(Long id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userId",id);
        modelAndView.setViewName("user/role");
        return modelAndView;
    }

    /**
     * 获取角色
     * @param userId
     * @return
     */
    @PostMapping(value = "getRoleTree",produces = "text/html;charset=UTF-8")
    public String getRoleTree(Long userId){
        List<TreeJson> treeJsons = roleService.getRoleTreeJsons(userId);
        String json="";
        try {
            json = JsonUtil.objectToJson(treeJsons);
        } catch (IOException e) {
            log.info("获取角色失败");
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping("addOrUpdateRoleAssign")
    public Object addOrUpdateRoleAssign(long userId,@RequestParam(value = "roles[]") String[] roles){
        userRoleService.addUserRoles(userId,roles);
        return ResultUtil.ok();
    }

}
